﻿function getHighChart() {
    $("#waitingMessage").show();
    //debugger;
    var chartID = $("#chartID").html();
    // Hidden in view
    var actionUrl = '/Home/GetChartData';
    var aUrl = '/Home/GetChartData2';
    //define the options
    var options = {
        chart: {
            renderTo: 'chart-container',
            zoomType: 'xy'
            //type: 'spline'
        },
        title: {
            text: 'Jón Kalmar' // Nafn á barni?
        },
        xAxis: {
            allowDecimals: false
        },
        yAxis: [{ // Primary
            title: {
                text: 'Þyngd'
            },
            opposite: true

        }, { // Secondary
            title: {
                text: 'Lengd'
            }
        }],
        legend: {
            layout: 'vertical',
            floating: true,
            backgroundColor: '#FFFFFF',
            align: 'right',
            verticalAlign: 'top',
            y: 60,
            x: -60
        },
        series: []
    };

    //Calls the JSON action method
    jQuery.getJSON(actionUrl, { chartID: chartID },
        function (points) {
            var series = {
                data: [],
                name: "Þyngd",
                type: "spline",
                yAxis: 1
            };

            //        var xAxis = {
            //            categories: []
            //        };

            $.each(points, function (pointNo, point) {
                series.data.push([point.Key, point.Value]);
            });
            options.series.push(series);
        });
        jQuery.getJSON(aUrl, { chartID: chartID},
            function (points){
                var series2 = {
                    data: [],
                    name: "Lengd",
                    type:"spline"
            };
            $.each(points, function(pointNo, point) {
                series2.data.push([point.Key, point.Value]);
            });
            options.series.push(series2);
            var chart = new Highcharts.Chart(options);
        });
//        jQuery.getJSON("/Home/GetTableData", { chartID: chartID },
//        function (data) {
//            ($("#data-template").tmpl(data).hide().prependTo("#chart-data").fadeIn("slow"));
//        });

        $("#waitingMessage").hide();
    }

    $(document).ready(function () {
        var chartID = $("#chartID").html();
        var ID = $("#ID").html();
        jQuery.getJSON("/Home/GetTableData", { chartID: chartID },
        function (data) {
            ($("#data-template").tmpl(data).hide().prependTo("#chart-data").fadeIn("slow"));
        });
        $("#submitBtn").click(function () {
            var x = $("#x").val();
            var y1 = $("#y1").val();
            var y2 = $("#y2").val();

            //            $("#x").attr("value", "");
            //            var x = { "x": x };
            if (1 != 1) {
                $("#commentError").show();
                return false;
            }
            else {
                $("#commentError").hide();
                $.post("/Home/CreateTimelineData/", { chartID: chartID, x: x, y1: y1, y2: y2 }, function (data) {
                    $(".row-data").before($("#data-template").tmpl(data).hide().fadeIn("slow"));
                    getHighChart();
                    //$("#x").focus();
                });
            }
        });

    });